﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExplorerPlus.DomainObjects;
using ExplorerPlus.PersistenceLayer;
using ExplorerPlus.Configuration;

namespace ExplorerPlus
{
    public partial class Preferences : Form
    {
        public Preferences()
        {
            InitializeComponent();
        }

        private void Preferences_Load(object sender, EventArgs e)
        {
            this.SuspendLayout();

            List<FlyWeightItem> categories = PersistenceLayerFacade.GetFlyWeightCategories();
            lstCategory.DataSource = categories;
            lstCategory.ValueMember = "Id";
            lstCategory.DisplayMember = "Name";
            lstCategory.SelectedValue = (int)Common.GetDefaultCategory();

            chkAlwaysShowTrayIcon.Checked = 
                ConfigurationSettings.AppSettings.AlwaysShowTrayIcon;
            chkCloseToTray.Checked =
                ConfigurationSettings.AppSettings.CloseToTray;
            chkConfirmForChangesMade.Checked =
                ConfigurationSettings.AppSettings.ConfirmForChangesMade;
            chkConfirmWhenDeletingItems.Checked =
                ConfigurationSettings.AppSettings.ConfirmWhenDeletingItems;
            chkMinimizeToTray.Checked =
                ConfigurationSettings.AppSettings.MinimizeToTray;
            chkSingleClickOnTrayToOpen.Checked =
                ConfigurationSettings.AppSettings.SingleClickOnTrayToOpen;
            chkAdvancedSearch.Checked =
                ConfigurationSettings.AppSettings.AdvancedSearch;
            chkAutoSave.Checked =
                ConfigurationSettings.AppSettings.AutoSave;

            lstCategoryFilters.Items.Add(
                new ListViewItem(
                    new string[] { "Books", ConfigurationSettings.AppSettings.BooksFilter }));
            lstCategoryFilters.Items.Add(
                new ListViewItem(
                    new string[] { "Games", ConfigurationSettings.AppSettings.GamesFilter }));
            lstCategoryFilters.Items.Add(
                new ListViewItem(
                    new string[] { "Movies", ConfigurationSettings.AppSettings.MoviesFilter }));
            lstCategoryFilters.Items.Add(
                new ListViewItem(
                    new string[] { "Music", ConfigurationSettings.AppSettings.MusicFilter }));

            lstAmazonLocales.Text = ConfigurationSettings.AppSettings.AmazonLocales;

            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            ConfigurationSettings.AppSettings.DefaultCategory =
                lstCategory.Text;
            ConfigurationSettings.AppSettings.AlwaysShowTrayIcon = 
                chkAlwaysShowTrayIcon.Checked;
            ConfigurationSettings.AppSettings.CloseToTray = 
                chkCloseToTray.Checked;
            ConfigurationSettings.AppSettings.ConfirmForChangesMade = 
                chkConfirmForChangesMade.Checked;
            ConfigurationSettings.AppSettings.ConfirmWhenDeletingItems = 
                chkConfirmWhenDeletingItems.Checked;
            ConfigurationSettings.AppSettings.MinimizeToTray = 
                chkMinimizeToTray.Checked;
            ConfigurationSettings.AppSettings.SingleClickOnTrayToOpen = 
                chkSingleClickOnTrayToOpen.Checked;
            ConfigurationSettings.AppSettings.AmazonLocales =
                lstAmazonLocales.Text;
            ConfigurationSettings.AppSettings.AdvancedSearch =
                chkAdvancedSearch.Checked;
            ConfigurationSettings.AppSettings.AutoSave =
                chkAutoSave.Checked;

            ConfigurationSettings.SaveChanges();

            this.DialogResult = DialogResult.OK;
        }
    }
}
